package top.oylan.manager.controller.mp;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import top.oylan.manager.entity.Category;
import top.oylan.manager.entity.Product;
import top.oylan.manager.entity.Video;
import top.oylan.manager.entity.mp.MpResult;
import top.oylan.manager.entity.mp.MpVideo;
import top.oylan.manager.service.CategoryService;
import top.oylan.manager.service.ProductService;
import top.oylan.manager.tools.Pager;
import top.oylan.manager.tools.SystemConstants;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-08-11
 */
@RestController
@RequestMapping("/mp/video")
@CrossOrigin
public class MpVideoController {

    @Resource
    ProductService productService;

    @Resource
    CategoryService categoryService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public MpResult index(@RequestParam(value = "page",defaultValue = "1")Integer page,
                        @RequestParam("type")Byte type,
                        @RequestParam(value = "cateId",required = false)Short cateId){

        Pager pager = new Pager<>();
        pager.setCurrentPage(page);
        pager.setEveyPageRow(SystemConstants.PAGER_FILM_ROW_SIZE);
        Product product = new Product();
        product.setType(type);
        if(cateId != null && cateId > 0){
            List<Category> categories = categoryService.selectCategoryByType((int) type);
            List<Short> shorts;
            shorts = categoryService.categoryTree(categories, new ArrayList<>(Arrays.asList((short) cateId)), cateId);
            product.setCateIdStr(shorts);
        }
        pager.setCondition(product);
        List<Video> list = productService.selectVideoByPager(pager);
        pager.setList(list);
        MpResult result = new MpResult();
        result.setData(pager);
        return result;
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public MpResult detail(@RequestParam("id")Integer id,
                           @RequestParam(value = "userId",defaultValue = "0")Integer userId){
        MpResult result = new MpResult();
        MpVideo video = productService.findVideoById(id,userId);
        int up = productService.watch(video.getId());
        if(up > 0){
            video.setWatched(video.getWatched()+1);
        }
        result.setData(video);
        return result;
    }

    @RequestMapping(value = "/detail/base",method = RequestMethod.GET)
    public MpResult base(@RequestParam("id")Integer id){
        MpResult result = new MpResult();
        Product product = productService.findBaseInfoById(id);
        result.setData(product);
        return result;
    }
}
